package ru.pstu.itas.cps.controls;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Button extends ActiveElement implements Touchable {

	int radius;
	Bitmap img;
	Bitmap img_pressed;

	public Button(String id, int x, int y, int radius, Bitmap img, Bitmap img_pressed) {
		super(id, x, y, 0);
		this.radius = radius;
		this.img = img;
		this.img_pressed = img_pressed;
	}

	@Override
	public void Draw(Canvas canvas) {
		Bitmap i = null;
		if (value == 0)
			i = img;
		else
			i = img_pressed;
		Rect srcRect = new Rect(0, 0, i.getWidth(), i.getHeight());
		Rect dstRect = new Rect(x - i.getWidth() / 2, y - i.getHeight() / 2, x + i.getWidth() / 2, y
				+ i.getHeight() / 2);
		canvas.drawBitmap(i, srcRect, dstRect, null);
	}

	public void TouchDown(float x, float y) {
		if ((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y) < radius * radius)
			setValue(1);
	}

	public void TouchMove(float x, float y) {
		if ((x - this.x) * (x - this.x) + (y - this.y) * (y - this.y) < radius * radius)
			setValue(1);
		else
			setValue(0);
	}

	public void TouchUp(float x, float y) {
		setValue(0);
	}

}
